#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));

    ui->widget->SetRenderWindow(viewer->getRenderWindow());
    viewer->setupInteractor(ui->widget->GetInteractor(), ui->widget->GetRenderWindow());
    ui->widget->update();

    qDebug()<<ui->menu->actions();

    connect(ui->menu->actions().at(0),&QAction::triggered,[=](bool tr){
        QString fileName = QFileDialog::getOpenFileName(this, ("Open File"),
                                                        "/home",
                                                        ("pcl files (*.pcd *.ply *.OBJ)"));
        qDebug()<<"add file"<<fileName;
        auto s = fileName.split("/");
        if(s.size() > 0){
            qDebug()<<s.at(s.size() - 1);
                //
            pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPoints(new pcl::PointCloud<pcl::PointXYZ>);
            if(s.at(s.size() - 1).contains(".pcd")){
                pcl::io::loadPCDFile(fileName.toStdString(), *cloudPoints);
            }else if(s.at(s.size() - 1).contains(".ply")){
                pcl::io::loadPLYFile(fileName.toStdString(), *cloudPoints);
            }else if(s.at(s.size() - 1).contains(".obj")){
                pcl::io::loadOBJFile(fileName.toStdString(), *cloudPoints);
            }

            //移除窗口点云
    //        viewer->removeAllPointClouds();
            //点云设置
            //设置点云颜色
            pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color(cloudPoints, 255, 255, 255);
            //点云颜色渲染
            viewer->addPointCloud(cloudPoints, cloud_color, s.at(s.size() - 1).toStdString());
            //设置点云大小
            viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, s.at(s.size() - 1).toStdString());
            viewer->resetCamera();
            ui->widget->update();
        }
    });
    qDebug()<<ui->menu->menuAction()->children();
}

MainWindow::~MainWindow()
{
    delete ui;
}

